package com.heima.wemedia.service.impl;

import com.heima.feign.article.IArticleClient;
import com.heima.feign.user.IUserClient;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentManageDto;

import com.heima.model.comment.dtos.CommentRepaySaveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.WmCommentDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WeMediaThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.pojos.*;
import com.heima.wemedia.service.WmcommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class WmcommentServiceImpl implements WmcommentService {
    @Autowired
    private IArticleClient articleClient;

    @Autowired
    private WmUserMapper wmUserMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUserClient iUserClient;


    /**
     * 查看所有评论
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findAllComment(WmCommentDto dto) {

        Integer wmUserId = WmThreadLocalUtils.getUser().getId();

        dto.setWmUserId(wmUserId);

        return articleClient.findAllComment(dto);
    }

    /**
     * 打开或者关闭评论
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult CloseAndOpenComment(CommentConfigDto dto) {
        //参数校验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询用户id
        Integer wmUser =WmThreadLocalUtils.getUser().getId();

        //根据id查询app用户id
        WmUser userId =wmUserMapper.selectById(wmUser);
        Integer apUserId = userId.getApUserId();

        //清空该文章的评论
        List<ApComment> apComments = mongoTemplate.find(Query.query(Criteria.where("entryId")
                .is(dto.getArticleId()).and("authorId").is(apUserId)), ApComment.class);

        for (ApComment apComment : apComments) {
            List<ApCommentRepay> apCommentRepays = mongoTemplate.find(Query.query(Criteria.where("commentId")
                    .is(apComment.getId()).and("authorId").is(apUserId)),ApCommentRepay.class);
            List<String> commentRepayIdList = apCommentRepays.stream().map(ApCommentRepay::getId)
                    .distinct().collect(Collectors.toList());

            //删除所有的评论回复点赞数据
            mongoTemplate.remove(Query.query(Criteria.where("commentRepayId")
                    .in(commentRepayIdList).and("authorId").is(apUserId)), ApCommentRepayLike.class);

            //删除该评论的所有的回复内容
            mongoTemplate.remove(Query.query(Criteria.where("entryId")
                    .is(dto.getArticleId()).and("authorId").is(apUserId)),ApCommentRepay.class);

            //删除评论的点赞
            mongoTemplate.remove(Query.query(Criteria.where("commentId").is(apComment.getId())
                    .and("authorId").is(apUserId)), ApCommentLike.class);

        }
        //删除评论
        mongoTemplate.remove(Query.query(Criteria.where("entryId").is(dto.getArticleId())
                .and("authorId").is(apUserId)),ApComment.class);

        //修改app文章的config配置
        return articleClient.updateCommentStatus(dto);
    }

    /**
     * 查看列表详情
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(CommentManageDto dto) {
        List<CommentRepayListVo> commentRepayListVoList = new ArrayList<>();

        Query query = Query.query(Criteria.where("entryId").is(dto.getArticleId()));
        Pageable pageable =  PageRequest.of(dto.getPage()-1,dto.getSize());
        query.with(pageable);
        query.with(Sort.by(Sort.Direction.DESC, "createdTime"));

        List<ApComment> list = mongoTemplate.find(query, ApComment.class);

        for (ApComment apComment : list) {
            CommentRepayListVo vo = new CommentRepayListVo();
            vo.setApComments(apComment);
            Query query2 = Query.query(Criteria.where("commentId").is(apComment.getId()));
            query2.with(Sort.by(Sort.Direction.DESC, "createdTime"));
            List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query2, ApCommentRepay.class);
            vo.setApCommentRepays(apCommentRepays);

            commentRepayListVoList.add(vo);
        }
        return ResponseResult.okResult(commentRepayListVoList);
    }

    /**
     * 评论回复
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult CommentRepay(CommentRepaySaveDto dto) {
        //参数校验
        if (dto == null || dto.getCommentId() == null || dto.getContent()== null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //评论不能超过140个字
        if (dto.getContent().length() > 140){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论不能超过140个字");
        }
        //获得自媒体人
        Integer wmUser =WmThreadLocalUtils.getUser().getId();
        WmUser  user =wmUserMapper.selectById(wmUser);
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取app端用户信息
        ApUser apUser =iUserClient.findUserById(user.getApUserId());
        //保存评论
        ApCommentRepay apCommentRepay =new ApCommentRepay();
        apCommentRepay.setAuthorId(apUser.getId());
        apCommentRepay.setAuthorName(apUser.getName());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setUpdatedTime(new Date());
        apCommentRepay.setLikes(0);
        mongoTemplate.save(apCommentRepay);
        //更新评论数量
        ApComment apComment =mongoTemplate.findById(dto.getCommentId(),ApComment.class);
        apComment.setReply(apComment.getReply()+1);
        mongoTemplate.save(apComment);
        //结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查看文章评论列表
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findNewsComments(ArticleCommentDto dto) {
        WmUser user = WmThreadLocalUtils.getUser();
        dto.setWmUserId(user.getId());
        return articleClient.findNewsComments(dto);
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @Override
    public ResponseResult delComment(String commentId) {
        if(StringUtils.isBlank(commentId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论id不能为空");
        }
        //删除评论
        mongoTemplate.remove(Query.query(Criteria.where("id").is(commentId)),ApComment.class);
        //删除该评论的所有的回复内容
        mongoTemplate.remove(Query.query(Criteria.where("commentId").is(commentId)),ApCommentRepay.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除评论回复
     * @param commentRepayId
     * @return
     */
    @Override
    public ResponseResult delCommentRepay(String commentRepayId) {
        if(StringUtils.isBlank(commentRepayId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论回复id不能为空");
        }
        mongoTemplate.remove(Query.query(Criteria.where("id").is(commentRepayId)),ApCommentRepay.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 作者点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(CommentLikeDto dto) {
        //1.检查参数
        if (dto == null || dto.getCommentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        WmUser wmUser = WmThreadLocalUtils.getUser();
        WmUser dbUser = wmUserMapper.selectById(wmUser.getId());
        if(dbUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取app端用户信息
        ApUser apUser = iUserClient.findUserById(dbUser.getApUserId());
        //3.点赞
        if (apComment != null && dto.getOperation() == 0) {
            //更新评论点赞数量
            apComment.setLikes(apComment.getLikes() + 1);
            mongoTemplate.save(apComment);
            //保存评论点赞数据
            ApCommentLike apCommentLike = new ApCommentLike();
            apCommentLike.setCommentId(apComment.getId());
            apCommentLike.setAuthorId(apUser.getId());
            mongoTemplate.save(apCommentLike);
        } else {
            //更新评论点赞数量
            int tmp = apComment.getLikes() - 1;
            tmp = tmp < 1 ? 0 : tmp;
            apComment.setLikes(tmp);
            mongoTemplate.save(apComment);
            //删除评论点赞
            Query query = Query.query(Criteria.where("commentId").is(apComment.getId()).and("authorId").is(apUser.getId()));
            mongoTemplate.remove(query, ApCommentLike.class);
        }
        //4.取消点赞
        Map<String, Object> result = new HashMap<>();
        result.put("likes", apComment.getLikes());
        return ResponseResult.okResult(result);
    }
}
